<%@tag pageEncoding="UTF-8"%>
<%@tag import="com.wondersgroup.wssip.tag.check.KeyValue"%>
<%@tag import="com.wondersgroup.framework.widget.library.extjs.form.ExtField"%>
<%@tag import="com.wondersgroup.framework.widget.library.extjs.form.ExtCheckbox"%>
<%@tag import="com.wondersgroup.framework.widget.taglib.form.FormConstants"%>
<%@tag import="com.wondersgroup.framework.widget.taglib.form.FormTag"%>
<%@tag import="java.util.List"%>

<!-- 属性申明 -->
<%@ attribute name="id" required="true" rtexprvalue="true" description="标签id"%>
<%@ attribute name="property" required="true" rtexprvalue="true" description="标签Property"%>
<%@ attribute name="data" required="true" type="java.util.List" rtexprvalue="true" description="标签所需数据"%>
<%@ attribute name="value" required="false" rtexprvalue="true" description="标签初始值"%>
<%@ attribute name="row" required="false" rtexprvalue="true" description="每行的check个数"%>
<%@ attribute name="readOnly" required="false" rtexprvalue="true" description="判断是否只读，默认false"%>
<%@ attribute name="disabled" required="false" rtexprvalue="true" description="判断是否禁用，默认false"%>
<%@ attribute name="required" required="false" rtexprvalue="true" description="判断是否是必输项，默认false"%>
<%@ attribute name="label" required="false" rtexprvalue="true" description="标签名称"%>

<!-- 引入第三方类库 -->
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<%
	//初始值处理
	String value = (String) getJspContext().getAttribute("value");
	String[] values = null;
	if (value != null) {
		values = value.split(",");
	} else {
		values = new String[0];
	}
	String sRow = (String) getJspContext().getAttribute("row");
	//每行显示
	int row = 4;
	if(sRow != null){
		row = Integer.parseInt(sRow);
	}
	int loop = 0;
%>

<table width="100%" id="${id}_table" cellspacing="0" cellpadding="0">
	<c:forEach items="${data}" var="ck" varStatus="index">
	<%
		ExtCheckbox ext = new ExtCheckbox(id);
		ext.setProperty(property);
		if (request.getAttribute(FormConstants.FORM_FIELDS) != null) {
			List<ExtField> fields = (List<ExtField>) request.getAttribute(FormConstants.FORM_FIELDS);
			fields.add(ext);
			request.setAttribute(FormConstants.FORM_FIELDS, fields);
		}
		String formItems = (String)request.getAttribute(FormConstants.FORM_ITEMS);
		if(loop % row == 0){
	%>
	<tr>
		<% 
			if(loop > 0){
		%>
		<td></td>
		<% 
			} 
		%>
	<%
		}
	%>
		<td height="20">
			<div id="${id}_${index.index}_div"></div>
			<script>
				Ext.onReady(function(){
					var objCk = new Ext.form.Checkbox({
						id : "${id}_${index.index}",
						renderTo : "${id}_${index.index}_div",
						name : "${property}",
						fieldLabel : "${label}",
						boxLabel : "${ck.value}",
						inputValue : "${ck.key}",
						readOnly : ("${readOnly}"=="true" ? true : false),
						disabled : ("${disabled}"=="true" ? true : false),
						allowBlank : ("${required}"=="true" ? false : true)
					});
					<%
						KeyValue kv =(KeyValue)this.getJspContext().getAttribute("ck");
						for(String k:values){
							if(k.equals(kv.getKey())){
					%>
								objCk.setValue(true);	
					<%
							}
						}
					%>
					<%=formItems%>.push(objCk);
				});		
			</script>
		</td>
		<%
			if (loop % row == row - 1) {
		%>
	</tr>
		<%
			}
			loop++;
		%>
	</c:forEach>
	<%
		if (loop % row > 0) {
	%>
	</tr>
	<%
		}
	%>
</table>
